//package com.cuijq.dubbo.common;
//
//
//import com.cuijq.dubbo.core.server.RpcEncoder;
//import com.cuijq.dubbo.demo.HelloService;
//import com.cuijq.dubbo.demo.HelloServiceImpl;
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.*;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.Map;
//
//public class ProviderApplication {
//    private static final int PORT = 8080;
//    private static final Map<String, Object> serviceMap = new HashMap<>();
//
//    static {
//        HelloService helloService = new HelloServiceImpl();
//        serviceMap.put(HelloService.class.getName(), helloService);
//    }
//
//    public static void main(String[] args) throws Exception {
//        NacosServiceRegistry serviceRegistry = new NacosServiceRegistry();
//        serviceRegistry.registerService(HelloService.class.getName(), "127.0.0.1", PORT);
//
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try {
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup, workerGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        public void initChannel(SocketChannel ch) throws Exception {
//                            ch.pipeline().addLast(new RpcEncoder());
//                            ch.pipeline().addLast(new RpcDecoder());
//                            ch.pipeline().addLast(new RpcServerHandler());
//                        }
//                    })
//                    .option(ChannelOption.SO_BACKLOG, 128)
//                    .childOption(ChannelOption.SO_KEEPALIVE, true);
//
//            ChannelFuture f = b.bind(PORT).sync();
//            f.channel().closeFuture().sync();
//        } finally {
//            workerGroup.shutdownGracefully();
//            bossGroup.shutdownGracefully();
//        }
//    }
//
//    static class RpcServerHandler extends ChannelInboundHandlerAdapter {
//        @Override
//        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//
//            RpcResponse response = (RpcResponse) msg;
//            try {
//                Object service = serviceMap.get(response.getClassName());
//                Method method = service.getClass().getMethod(response.getMethodName(), response.getParameterTypes());
//                Object result = method.invoke(service, response.getParameters());
//                response.setResult(result);
//            } catch (Exception e) {
//                response.setError(e);
//            }
//            ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);;
//
////            RpcResponse request = (RpcResponse) msg;
////            RpcResponse response = new RpcResponse();
////            response.setRequestId(request.getRequestId());
////            try {
////                Object service = serviceMap.get(request.getClassName());
////                Method method = service.getClass().getMethod(request.getMethodName(), request.getParameterTypes());
////                Object result = method.invoke(service, request.getParameters());
////                response.setResult(result);
////            } catch (Exception e) {
////                response.setError(e);
////            }
////            ctx.writeAndFlush(response);
//            //.addListener(ChannelFutureListener.CLOSE);;
//        }
//
//        @Override
//        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//            cause.printStackTrace();
//            ctx.close();
//        }
//    }
//}